package com.alex.xmlyfm.fragments.discover;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.alex.mylibrary.tasks.TaskCallBack;
import com.alex.mylibrary.tasks.TaskResult;
import com.alex.mylibrary.utils.DimensionUtil;
import com.alex.xmlyfm.AlbumDetailActivity;
import com.alex.xmlyfm.Constants;
import com.alex.xmlyfm.R;
import com.alex.xmlyfm.adapters.DiscoverRecommendAdapter;
import com.alex.xmlyfm.adapters.PicPagerAdapter;
import com.alex.xmlyfm.entities.discoverrecommend.AlbumRecommend;
import com.alex.xmlyfm.entities.discoverrecommend.DiscoverRecommendAlbums;
import com.alex.xmlyfm.entities.discoverrecommend.DiscoverRecommendItem;
import com.alex.xmlyfm.fragments.BaseFragment;
import com.alex.xmlyfm.tasks.DiscoverRecommendTask;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverRecommendFragment extends BaseFragment implements TaskCallBack, View.OnClickListener {


    private DiscoverRecommendAdapter adapter;
    private List<DiscoverRecommendItem> items;
    private LinkedList picData = new LinkedList();

    private ViewPager focusImagesPager;
    private PicPagerAdapter picPagerAdapter;

    public DiscoverRecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        items = new LinkedList<>();
        adapter = new DiscoverRecommendAdapter(getContext(), items);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_recommend, container, false);
        ListView listView = (ListView) ret.findViewById(R.id.discover_recommend_list);
        // 创建 ViewPager
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                DimensionUtil.dp2px(getContext(),200)
        );

        focusImagesPager = new ViewPager(container.getContext());
        focusImagesPager.setLayoutParams(lp);



        ///////////////
        picPagerAdapter = new PicPagerAdapter(picData,getContext());
        focusImagesPager.setAdapter(picPagerAdapter);
        listView.addHeaderView(focusImagesPager);

        // 点击事件

        adapter.setOnRecommendAlbumClickListener(this);

        listView.setAdapter(adapter);


        return ret;
    }


    /**
     * 非此进入触发刷新
     */
    @Override
    public void onResume() {
        super.onResume();
        DiscoverRecommendTask task = new DiscoverRecommendTask(this);
        task.execute();
    }

    @Override
    public String getFragmentTitle() {
        return "推荐";
    }

    @Override
    public void onTaskFinished(TaskResult result) {
        // TODO 处理推荐列表数据
        if (result != null){
            if (result.action == Constants.TASK_ACTION_RECOMMENDS){
                if (result.resultCode == Constants.TASK_RESULT_OK){
                    Object data = result.data;
                    if (data != null && data instanceof List){
                        List list = (List) data;
                        // TODO 更新 ListViewAdapter

                        // 只要数据来了就清除 adapter
                        items.clear();
                        for (Object o : list) {
                            if (o instanceof DiscoverRecommendItem){
                                items.add((DiscoverRecommendItem)o);
                            }
                        }
                        Log.d("discover",""+items.size());
                        adapter.notifyDataSetChanged();

                        picData.addAll((LinkedList) result.datas[0]);
                        picPagerAdapter.notifyDataSetChanged();
                        focusImagesPager.setCurrentItem(20);
                    }
                }else {
                    // TODO 加载失败
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag != null) {
            if (tag instanceof String){
                String str = (String) tag;
                if (str.startsWith(Constants.TAG_DISCOVER_RECOMMEND_ALBUM)){
                    //TODO 需要处理专辑推荐的图标点击
                    str = str.substring(Constants.TAG_DISCOVER_RECOMMEND_ALBUM.length());

                    int index = str.indexOf(':');
                    if (index > -1){
                        String s1 = str.substring(0,index);
                        String s2 = str.substring(index +1);

                        int position = Integer.parseInt(s1);
                        int child = Integer.parseInt(s2);
                        DiscoverRecommendAlbums albums = (DiscoverRecommendAlbums) items.get(position);
                        List<AlbumRecommend> recommends = albums.getAlbumRecommends();

                        if (recommends != null) {
                            AlbumRecommend albumRecommend = recommends.get(child);
                            // 专辑ID
                            long albumId = albumRecommend.getAlbumId();

                            Intent intent = new Intent(getContext(), AlbumDetailActivity.class);
                            intent.putExtra(Constants.ALBUM_ID, albumId);
                            startActivity(intent);



                            // 曲目 ID
                            long trackId = albumRecommend.getTrackId();

                        }
                    }


                }
            }
        }

    }
}
